package com.avic.controller;

import com.avic.common.constant.SystemLogConstant;
import com.avic.common.constant.UserConstant;
import com.avic.common.convertor.FleetResourceConvertor;
import com.avic.common.util.AssertUtil;
import com.avic.common.util.ex.BizErrorCode;
import com.avic.common.util.lang.BaseResponse;
import com.avic.common.util.lang.ServiceCallBack;
import com.avic.controller.request.CreateFleetResourceReq;
import com.avic.controller.request.GetFleetResourceListReq;
import com.avic.controller.request.UpdateFleetResourceReq;
import com.avic.controller.result.BaseCreateResourceRes;
import com.avic.controller.result.GetFleetResourceListRes;
import com.avic.controller.result.GetFleetResourceSelectRes;
import com.avic.dal.condition.QueryFleetResourceListCondition;
import com.avic.dal.model.FleetResource;
import com.avic.dal.model.vo.FleetResourceListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("resource/fleet")
@Api(tags = "车队资源", description = "查询车对资源列表&创建车对资源&修改车对资源&删除车对资源&获取所有车队信息")
public class FleetResourceController extends AbstractFacadeImpl {

    @ApiOperation(value = "查询车辆资源列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "getFleetResourceListReq", required = true, dataType = "GetFleetResourceListReq")
    })
    @PostMapping("list")
    public GetFleetResourceListRes getFleetResourceList(@RequestBody GetFleetResourceListReq getFleetResourceListReq) {
        final GetFleetResourceListRes result = new GetFleetResourceListRes();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
            }

            @Override
            public void doService() throws Exception {
                QueryFleetResourceListCondition queryFleetResourceListCondition = new QueryFleetResourceListCondition();
                queryFleetResourceListCondition.setFleetName(getFleetResourceListReq.getFleetName());

                List<FleetResourceListVO> fleetResourceListVOS = fleetResourceService.itemList(queryFleetResourceListCondition);

                result.setItemList(FleetResourceConvertor.fleetResourceListVOToFleetResourceListInfo(fleetResourceListVOS));
                result.setSuccess(true);
            }
        });
    }

    @ApiOperation(value = "创建车队资源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "createFleetResourceReq", required = true, dataType = "CreateFleetResourceReq")
    })
    @PostMapping
    public BaseCreateResourceRes createFleetResource(@RequestBody CreateFleetResourceReq createFleetResourceReq,
                                                     @RequestHeader(value = UserConstant.HEADER_CURRENT_USER_ID_KEY, required = false) String currentUserId) {
        final BaseCreateResourceRes result = new BaseCreateResourceRes();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
                AssertUtil.isNotBlank(createFleetResourceReq.getFleetName(), "fleetName is blank");

            }

            @Override
            public void doService() throws Exception {
                FleetResource fleetResource = new FleetResource();
                fleetResource.setFleetName(createFleetResourceReq.getFleetName());

                result.setItemId(String.valueOf(fleetResourceService.create(fleetResource)));

                // 新建系统日志
                systemServiceFeignService.createSystemLog(
                        String.format(SystemLogConstant.CREATE_FLEET_RESOURCE, result.getItemId(), objectMapper.writeValueAsString(createFleetResourceReq)), currentUserId);
                result.setSuccess(true);
            }
        });
    }

    @ApiOperation(value = "修改车队资源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "updateFleetResourceReq", required = true, dataType = "UpdateFleetResourceReq")
    })
    @PutMapping
    public BaseResponse updateSingleSoldier(@RequestBody UpdateFleetResourceReq updateFleetResourceReq,
                                            @RequestHeader(value = UserConstant.HEADER_CURRENT_USER_ID_KEY, required = false) String currentUserId) {
        final BaseResponse result = new BaseResponse();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
                AssertUtil.isTrue(updateFleetResourceReq.getItemId() > 0, BizErrorCode.PARAM_ERROR, "itemId不能小于零");
                AssertUtil.isNotBlank(updateFleetResourceReq.getFleetName(), "fleetName is blank");

            }

            @Override
            public void doService() throws Exception {

                FleetResource fleetResource = new FleetResource();
                fleetResource.setFleetName(updateFleetResourceReq.getFleetName());
                fleetResource.setId(updateFleetResourceReq.getItemId());

                fleetResourceService.update(fleetResource);

                // 新建系统日志
                systemServiceFeignService.createSystemLog(
                        String.format(SystemLogConstant.UPDATE_FLEET_RESOURCE, updateFleetResourceReq.getItemId()
                                , objectMapper.writeValueAsString(updateFleetResourceReq)), currentUserId);
                result.setSuccess(true);
            }
        });
    }

    @ApiOperation(value = "删除车对资源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "itemId", paramType = "path", value = "资源ID", required = true)
    })
    @DeleteMapping("/{itemId}")
    public BaseResponse updateSingleSoldier(@PathVariable("itemId") int itemId,
                                            @RequestHeader(value = UserConstant.HEADER_CURRENT_USER_ID_KEY, required = false) String currentUserId) {
        final BaseResponse result = new BaseResponse();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
                AssertUtil.isTrue(itemId > 0, BizErrorCode.PARAM_ERROR, "数据ID大于零");

            }

            @Override
            public void doService() throws Exception {

                fleetResourceService.delete(itemId);
                // 新建系统日志
                systemServiceFeignService.createSystemLog(
                        String.format(SystemLogConstant.DELETE_FLEET_RESOURCE, itemId), currentUserId);
                result.setSuccess(true);
            }
        });
    }

    @ApiOperation(value = "获取所有车队信息", notes = "用户新增车辆信息时，车队下拉数据")
    @GetMapping("all")
    public GetFleetResourceSelectRes getFleetResourceList() {
        final GetFleetResourceSelectRes result = new GetFleetResourceSelectRes();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
            }

            @Override
            public void doService() throws Exception {

                List<FleetResource> fleetResourceList = fleetResourceService.all();
                result.setItemList(FleetResourceConvertor.fleetResourceListToFleetResourceSelectInfo(fleetResourceList));
                result.setSuccess(true);
            }
        });
    }
}
